using System;
using System.Data;
using System.Web;
using BBX.Common;
using BBX.Entity;
using BBX.Forum;
using BBX.Plugin.Payment;
using BBX.Plugin.Payment.Alipay;
using BBX.Web.UI;

namespace BBX.Web
{
    public class usercpcreditsbuy : UserCpPage
    {
        public Single creditstax = Scoresets.GetCreditsTax();
        public String jscreditsratearray = "<script type=\"text/javascript\">\r\nvar creditsrate = new Array();\r\n{0}\r\n</script>";
        public Int32 creditstrans = Scoresets.GetCreditsTrans();
        public String creditstransname = Scoresets.GetValidScoreName()[Scoresets.GetCreditsTrans()];
        public String creditstransunit = Scoresets.GetValidScoreUnit()[Scoresets.GetCreditsTrans()];
        public Int32 creditsamount = DNTRequest.GetInt("amount", 1);

        protected override void ShowPage()
        {
            pagetitle = "积分充值";
            if (!EPayments.IsOpenEPayments())
            {
                base.AddErrLine("论坛未开启积分充值服务！");
                return;
            }
            var text = "";
            foreach (DataRow dataRow in Scoresets.GetScorePaySet(0).Rows)
            {
                Object obj = text;
                text = obj + "creditsrate[" + dataRow["id"] + "] = " + dataRow["rate"] + ";\r\n";
            }
            jscreditsratearray = string.Format(jscreditsratearray, text);
            if (!IsLogin())
            {
                return;
            }
            String @string;
            if (!string.IsNullOrEmpty(DNTRequest.GetString("redirect")) && (@string = DNTRequest.GetString("redirect")) != null)
            {
                if (!(@string == "alipay"))
                {
                    return;
                }
                RedirectToAlipay();
            }
        }

        public void RedirectToAlipay()
        {
            var digitalTrade = new DigitalTrade();
            digitalTrade.Subject = string.Format("{0} 论坛积分充值({1}:{2}{3}),用户:{4}", new Object[]
			{
                config.Forumtitle,
                creditstransname,
                creditsamount,
                creditstransunit,
                username
            });
            if (Utils.IsValidEmail(config.Alipayaccout))
            {
                digitalTrade.Seller_Email = config.Alipayaccout;
            }
            else
            {
                digitalTrade.Seller_Id = config.Alipayaccout;
            }
            digitalTrade.Return_Url = Utils.GetRootUrl(forumpath) + "tools/notifypage.aspx";
            digitalTrade.Notify_Url = Utils.GetRootUrl(forumpath) + "tools/notifypage.aspx";
            digitalTrade.Quantity = 1;
            var num = decimal.Round(creditsamount / config.Cashtocreditrate, 2);
            digitalTrade.Price = ((num > 0.1m) ? num : 0.1m);
            digitalTrade.Payment_Type = 1;
            digitalTrade.PayMethod = "bankPay";
            digitalTrade.Partner = config.Alipaypartnerid;
            digitalTrade.Sign = config.Alipaypartnercheckkey;
            var url = StandardAliPayment.GetService().CreateDigitalGoodsTradeUrl(digitalTrade);
            //CreditOrders.CreateCreditOrder(this.userid, this.username, this.creditstrans, this.creditsamount, 1, digitalTrade.Out_Trade_No);
            Order.Create(userid, username, creditstrans, creditsamount, 1, digitalTrade.Out_Trade_No);

            HttpContext.Current.Response.Redirect(url);
        }
    }
}